<%-- 
    Document   : CadastrarCategorias
    Created on : 15/09/2008, 15:40:21
    Author     : 2006244090114
--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="java.util.*,persistencia.*, modelo.*"contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" href="gastozero.css" type="text/css">
    <title>SUBCATEGORIAS</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <script language="javascript">
        function criaXMLHttp() {
            if (typeof XMLHttpRequest != "undefined"){
                return new XMLHttpRequest();
            }else if (window.ActiveXObject){
                var versoes = ["MSXML2.XMLHttp.5.0",
                    "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0",
                    "MSXML2.XMLHttp", "Microsoft.XMLHttp"
                ];
            }
            for (var i = 0; i < versoes.length; i++){
                try{
                    return new ActiveXObject(versoes[i]);
                }catch (e) {}
            }
            throw new Error("Seu browser nao suporta AJAX");
        }
    
        function recuperaSubCategorias(campo, campo2) {
            var XMLHttp = criaXMLHttp();
        
            var url ="recuperaSubcategorias.jsp?id_categoria=" + campo.value;
            XMLHttp.open("get", url, true);
            XMLHttp.onreadystatechange = function () {
                if (XMLHttp.readyState == 4)
                    if (XMLHttp.status == 200){
                        var retorno = XMLHttp.responseText;
                        alert(retorno);
                        arr_opcoes = retorno.split(',');
                        for (i=0;i<arr_opcoes.length;i++) {
                            arr_opcao = arr_opcoes[i].split('|');
                            id_subcategoria = arr_opcao[0];
                            nome_subcategoria = arr_opcao[1];
                            document.getElementById(campo2).options[i] = new Option(nome_subcategoria,id_subcategoria);
                        }
                } else {
                    alert("Um erro ocorreu" + XMLHttp.statusText);
                }
            };
            XMLHttp.send(null);            
        }
    </script>
</head>
<body  class="body" background="imagemDeFundo.jpg">
<center> <font size="5" face="arial" > <h1>Gasto Zero</h1></font></center><br/><br/><br/>
<center>
    <h3>CONFIGURACAO DAS SUBCATEGORIAS</h3>
</center>
<div class="style1">
    <center>
        <font ><a href="Menu.jsp" class="texto" >MENU</a></font>&nbsp;&nbsp;&nbsp;&nbsp;
        <font  class="texto"><a class="texto"  href="ServletSubcategoria?acao=listar">EDITAR </a></font>&nbsp;&nbsp;&nbsp;&nbsp;
        <font  class="texto"><a class="texto"  href="index.jsp">SAIR >></a></font>&nbsp;&nbsp;&nbsp;&nbsp;
    </center>
</div>
<div class="style3">
<form action="ServletSubcategoria?acao=salvar" method="POST">  
<center>   
    <%
            // tirar todo esse trecho e colocar em um servlet
            CategoriaDAO categoriaDAO = new CategoriaDAO();
            Vector cat = categoriaDAO.getCategorias();
            Iterator categorias = cat.iterator();
            // no servlet isso será um request.setAttribute... 
            pageContext.setAttribute("categorias", categorias);
    %>         
    <h3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Escolha a Categoria para a nova subcategoria:&nbsp;</h3>   
    <select name="categorias" id="categorias">
        <c:forEach var="categoria"   items="${categorias}">
            <option value="${categoria.id_categoria}">
                ${categoria} 
            </option>
        </c:forEach>
    </select>       
    <h4>Nova Subcategoria: </h4><input type="text" name="nome_subcategoria" value=""/><input type="submit" value="Salvar" />${msg}
</center>
</form>  
</div>       
</body>
</html>
